package com.sbtr.util;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;


public class ImgUtil {
	private ImgUtil() {}
	private static final String WIDTH = "width";
    private static final String HEIGHT = "height";
    private static final String FORMAT = "format";
    private static final String IMG_FORMAT_RGBA = "RGBA";
    private static final String IMG_FORMAT_RGB = "RGB";
    
    public static Map<String, String> getImageMetadata(byte[] img) {
        Map<String, String> result = new HashMap<>();
        BufferedImage bimg = null;
        try {
            bimg = ImageIO.read(new ByteArrayInputStream(img));
        } catch (IOException e) {
            throw new RuntimeException("get image metadata error", e);
        }
        int width = bimg.getWidth();
        int height = bimg.getHeight();
        String format = bimg.getTransparency() == Transparency.OPAQUE ? 
                IMG_FORMAT_RGB : IMG_FORMAT_RGBA;
        result.put(WIDTH, width + "");
        result.put(HEIGHT, height + "");
        result.put(FORMAT, format);
        return result;
    }
}
